#!/bin/bash

if [ -z "${autoplat_script_type}" ]; then
    echo "script_type can't be empty"
    exit 1
fi

if [ -z "${autoplat_chrome_cdp_url}" ]; then
    echo "chrome_cdp_url can't be empty"
    exit 1
fi

if [ -z "${autoplat_task_id}" ]; then
    echo "task_id can't be empty"
    exit 1
fi

handle_ps() {
    if [ -z "${autoplat_search_start_date}" ]; then
        echo "search_start_date can't be empty"
        exit 1
    fi
    
    if [ -z "${autoplat_search_end_date}" ]; then
        echo "search_start_date can't be empty"
        exit 1
    fi

    local task_id="$1"

    exec crawler -c ps_config_pro.yaml --script_param.script_type="ps" \
                                       --script_param.task_id="${task_id}" \
                                       --script_param.search.start_date="${autoplat_search_start_date}" \
                                       --script_param.search.end_date="${autoplat_search_end_date}" \
                                       --playwright.params.endpoint_url="${autoplat_chrome_cdp_url}"
}

handle_sms() {
    if [ -z "${autoplat_search_start_date}" ]; then
        echo "search_start_date can't be empty"
        exit 1
    fi
    
    if [ -z "${autoplat_search_end_date}" ]; then
        echo "search_start_date can't be empty"
        exit 1
    fi

    if [ -z "${autoplat_search_send_strategy}" ]; then
        echo "search_send_strategy can't be empty"
        exit 1
    fi

    if [ -z "${autoplat_search_status}" ]; then
        echo "search_send_status can't be empty"
        exit 1
    fi

    local task_id="$1"

    exec crawler -c sms_config_pro.yaml --script_param.script_type="sms" \
                                        --script_param.task_id="${task_id}" \
                                        --script_param.search.start_date="${autoplat_search_start_date}" \
                                        --script_param.search.end_date="${autoplat_search_end_date}" \
                                        --script_param.search.send_strategy="${autoplat_search_send_strategy}" \
                                        --script_param.search.status="${autoplat_search_status}" \
                                        --playwright.params.endpoint_url="${autoplat_chrome_cdp_url}"
}


case "${autoplat_script_type}" in
    "ps")  handle_ps  "${autoplat_task_id}" || exit 1 ;;
    "sms") handle_sms "${autoplat_task_id}" || exit 1 ;;
    *)
        echo "错误：未知的 autoplat_script_type 值 '${autoplat_script_type}'，允许的值为 ps 或 sms"
        exit 1
        ;;
esac

exit 0
